package com.dtech.submp.dao;


import com.dtech.submp.common.nutzdao.CmsDao;
import com.dtech.submp.model.UsersEntity;
import org.nutz.dao.*;
import org.nutz.dao.pager.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * 用户信息查询Dao层
 * 
 * @ClassName: UserDao
 * @author wangxp
 * @date 2018年7月6日 下午2:49:22
 *
 */
@Component
public class UsersDao {
	@Autowired
	private CmsDao dao;
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	/**
	 * 单条查询
	 */
	public UsersEntity findById(String id) {

		Condition cnd = Cnd.where("id", "=", id);

		return dao.fetch(UsersEntity.class, cnd);
	}

	/**
	 * 添加用户
	 */
	public UsersEntity insert(UsersEntity entity) {
		return dao.insert(entity);
	}

	/**
	 * 修改用户
	 */
	public int update(UsersEntity entity) {
		Condition cnd = Cnd.where("id", "=", entity.getId());
		Chain chain = Chain.make("create_time", entity.getCreateTime());

		if (entity.getTel() != null) {
			chain.add("tel", entity.getTel());
		}
		return dao.update(UsersEntity.class, chain, cnd);
	}

	/**
	 * 查询用户列表
	 */
	public QueryResult findUserList(UsersEntity entity, int pageNumber, int pageSize){
		Pager pager = dao.createPager(pageNumber, pageSize);
		List<UsersEntity> list = dao.query(UsersEntity.class, null, pager);
		pager.setRecordCount(dao.count(UsersEntity.class));
		return new QueryResult(list, pager);
	}

}
